#include "../mqserver/mq_exchange.hpp"
#include <gtest/gtest.h>

mqserver::ExchangeManagerPtr exmp;

class ExchangeTest : public testing::Environment 
{
public:
    virtual void SetUp() override { 
        exmp = std::make_shared<mqserver::ExchangeManager>("./data/meta.db");
    }
    virtual void TearDown() override { /* exmp->clear(); */ std::cout << "clear finish!\n"; }
};

TEST(exchange_test, insert_test)
{
    // std::unordered_map<std::string, std::string> map = {
    //     {"kv1", "va1"}, {"kv2", "va2"}
    // };
    std::unordered_map<std::string, std::string> map;
    exmp->declareExchange("exch1", proto::message::ExchangeType::DIRECT, true, false, map);
    exmp->declareExchange("exch2", proto::message::ExchangeType::DIRECT, true, false, map);
    exmp->declareExchange("exch3", proto::message::ExchangeType::DIRECT, true, false, map);
    exmp->declareExchange("exch4", proto::message::ExchangeType::DIRECT, true, false, map);
    ASSERT_EQ(exmp->size(), 4); 
}

TEST(exchange_test, select_test)
{
    ASSERT_EQ(exmp->exists("exch1"), true);
    ASSERT_EQ(exmp->exists("exch2"), false);
    ASSERT_EQ(exmp->exists("exch3"), true);
    ASSERT_EQ(exmp->exists("exch4"), true);
    mqserver::ExchangePtr exp = exmp->selectExchange("exch3");
    ASSERT_NE(exp.get(), nullptr);
    ASSERT_EQ(exp->name_, "exch3");
    ASSERT_EQ(exp->type_, proto::message::ExchangeType::DIRECT);
    ASSERT_EQ(exp->durable_, true);
    ASSERT_EQ(exp->auto_delete_, false);
    ASSERT_EQ(exp->getArgs(), std::string("kv1=va1&kv2=va2&"));
}

TEST(exchange_test, remove_test)
{
    exmp->deleteExchange("exch3");
    mqserver::ExchangePtr exp = exmp->selectExchange("exch3");
    ASSERT_EQ(exp.get(), nullptr);
    ASSERT_EQ(exmp->exists("exch3"), false);
}

int main(int argc,char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest());
    (void)RUN_ALL_TESTS();
    return 0;
}